package com.coldworks.coldjoke.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.coldworks.coldjoke.R;

/**
 * 菜单适配器，展示出选项
 */

public class MenuAdapter extends BaseAdapter {

	private List<Object> items;
	private Context context;
	private int categoryRes = R.layout.menu_row_default_category;
	private int itemRes = R.layout.menu_row_default_item;

	public MenuAdapter(Context context, List<Object> items) {
		this.items = items;
		this.context = context;
	}

	public MenuAdapter(Context context, List<Object> items, int categoryRes) {
		this.context = context;
		this.items = items;
		this.categoryRes = categoryRes;
	}

	public MenuAdapter(Context context, List<Object> items, int categoryRes,
			int itemRes) {
		this.context = context;
		this.items = items;
		this.itemRes = itemRes;
		this.categoryRes = categoryRes;
	}

	@Override
	public int getCount() {
		return this.items.size();
	}

	@Override
	public Object getItem(int position) {
		return this.items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		return getItem(position) instanceof MItem ? 0 : 1;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public boolean isEnabled(int position) {
		return getItem(position) instanceof MItem;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		Object item = getItem(position);

		if (item instanceof Category) {
			if (v == null) {
				v = LayoutInflater.from(context).inflate(categoryRes, parent,
						false);
			}

			((TextView) v).setText(((Category) item).mTitle);

		} else {
			if (v == null) {
				v = LayoutInflater.from(context)
						.inflate(itemRes, parent, false);
			}

			TextView tv = (TextView) v;
			tv.setText(((MItem) item).mTitle);
			tv.setCompoundDrawablesWithIntrinsicBounds(((MItem) item).mIconRes,
					0, 0, 0);
		}

		v.setTag(R.id.mdActiveViewPosition, position);

		// if (position == mActivePosition) {
		// mMenuDrawer.setActiveView(v, position);
		// }

		return v;
	}

	/**
	 * 
	 * 菜单项
	 * 
	 */
	public static final class MItem {

		String mTitle;
		int mIconRes;

		public MItem(String title, int iconRes) {
			mTitle = title;
			mIconRes = iconRes;
		}
	}

	/**
	 * 
	 * 菜单分割线
	 * 
	 */
	public static final class Category {

		String mTitle;

		public Category(String title) {
			mTitle = title;
		}
	}

}
